from scapy.all import *
con_iface= 'enp3s15'
con_iface= 'eth0'
sock = conf.L2socket(iface=con_iface)
msg = b'a'*16+b'x'*1000
port = 8899
import threading
lock = threading.Lock()
import random
uTime = time.monotonic()
tt = None
sock2 = conf.L2socket(iface=con_iface)

def xx():
    while True:
        time.sleep(1)
        lock.acquire()
        lock.release()
        abc = tt
        if abc:
            for i in range(100):
                sock2.send(abc)
t = threading.Thread(target=xx)
t.setDaemon(True)
t.start()
co = 0
def aa(p):
    global tt,uTime,co
    t = p[TCP]
    i = p[IP]
    ip = IP(dst=i.src)
    d = t.payload.original
    if 'S' in t.flags:            
        tcp = Ether()/ip/TCP(sport=port,dport=t.sport,seq=random.randint(3425,0xffffffff-4545),ack=t.seq+1,flags='SA',window=60000)
        sock.send(tcp)
        return
    if 'F' in t.flags or 'R' in t.flags:
        return
    
    if d[:16]!=b'a'*16:
        return
    co += 1
    print (co)
    tcp = Ether()/ip/TCP(sport=port,dport=t.sport,seq=t.ack,ack=t.seq+len(d),flags='PA',window=60000)/msg
    sock.send(tcp)   
    if time.monotonic()-uTime>1:
        uTime = time.monotonic()
        lock.acquire()
        tt = tcp
        lock.release()
ico = 0
def icmpF(p):
    global ico
    ico += 1
    print ('icmp',ico)
    
def qq():
    a = sniff(prn=icmpF, filter="icmp" ,store=0,iface=con_iface)
t2 = threading.Thread(target=qq)
t2.setDaemon(True)
t2.start()
a = sniff(prn=aa, filter="tcp and dst port %s"%port ,store=0,iface=con_iface)


